import axios from 'axios'
import knex from './myKenx.js'
const service = axios.create()

export default function () {
  // 清空当前数据库表
  knex('herolist')
    .delete()
    .then(res => {
      console.log('清空herolist表成功', res)
    })
    .catch(err => {
      return console.log('清空失败', err)
    })
  //重新从lol官网拉取最新英雄数据
  service
    .get('https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js', {
      method: 'get'
    })
    .then(res => {
      console.log(res)
      res.data.hero.forEach(element => {
        element.iconImg = `http://game.gtimg.cn/images/lol/act/img/champion/${element.alias}.png`
        element.bannerImg = `https://game.gtimg.cn/images/lol/act/img/skin/big${element.heroId}000.jpg`
        element.roles = element.roles + ''
      })
      res.data.hero.forEach(element => {
        knex('herolist')
          .insert({ ...element })
          .then(res => {
            // console.log('res:', res)
          })
          .catch(err => {
            return console.log(err)
          })
      })
    })
}
